#include "stdafx.h"
#include <stdio.h>
#include "Thread.h"


CThread::CThread()
{
	secDescr = new SECURITY_DESCRIPTOR();
	secAttr = new SECURITY_ATTRIBUTES();
	pea = new EXPLICIT_ACCESS();

	stackSize = 1024;
	flags = CREATE_SUSPENDED;

	secAttr->nLength = sizeof(SECURITY_ATTRIBUTES);
	secAttr->lpSecurityDescriptor = secDescr;

	SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
	SIDAuthNT = SECURITY_NT_AUTHORITY;

	createDescriptor();

	threadHandle = CreateThread(secAttr, stackSize, (LPTHREAD_START_ROUTINE)CThread::comm_routine, NULL, flags, &threadID);
}


CThread::~CThread()
{
	delete (pea);
	delete (secAttr);
	delete (secDescr);
}


void CThread::createDescriptor()
{
	int ret;

	ret = AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID);

	ZeroMemory(pea, sizeof(EXPLICIT_ACCESS));
	pea->grfAccessPermissions = MAXIMUM_ALLOWED /*KEY_READ*/;
	pea->grfAccessMode = GRANT_ACCESS /*| SET_ACCESS*/;
	pea->grfInheritance = NO_INHERITANCE;
	pea->Trustee.TrusteeForm = TRUSTEE_IS_SID;
	pea->Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
	pea->Trustee.ptstrName = (LPTSTR)pEveryoneSID;

	ret = SetEntriesInAcl(1, pea, NULL, &pACL);

	printf("%d", ret);

	pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);

	ret = InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION);

	ret = SetSecurityDescriptorDacl(pSD, TRUE, pACL, FALSE);
}


void CThread::mainroutine(LPVOID parameters)
{

}

void CThread::comm_routine(LPVOID p_in_thread)
{

}